﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Text;

namespace OnlineManagement.Common
{
    public class JsonHelper
    {
        /// <summary>
        /// create json data according to entity
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static string GetJsonSerializar<T>(T t)
        {
            var ser = new DataContractJsonSerializer(typeof(T));
            using (MemoryStream ms = new MemoryStream())
            {
                ser.WriteObject(ms, t);
                return Encoding.UTF8.GetString(ms.ToArray());
            }
        }
        /// <summary>
        /// parse json data accroding to json string data
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="jsonStr"></param>
        /// <returns></returns>
        public static T ParseFromJson<T>(string jsonStr)
        {
            //T obj = Activator.CreateInstance<T>();
            //using (MemoryStream ms=new MemoryStream(Encoding.UTF8.GetBytes(jsonStr)))
            //{
            //    DataContractJsonSerializer jsonSerializer=new DataContractJsonSerializer(obj.GetType());
            //    return (T) jsonSerializer.ReadObject(ms);
            //}
            var ser = new DataContractJsonSerializer(typeof(T));
            var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonStr));
            var obj = (T)ser.ReadObject(ms);
            return obj;
        }

        private static string DataSetToJson(string jsonName,System.Data.DataSet ds) {
            StringBuilder Json = new StringBuilder();
            Json.Append("{\"" + jsonName + "\":[");
            if(ds!=null&&ds.Tables!=null){
                foreach (System.Data.DataTable dt in ds.Tables)
                {
                    if (dt.Rows.Count > 0)
                    {
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            Json.Append("{");
                            for (int j = 0; j < dt.Columns.Count; j++)
                            {
                                Json.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":\"" + dt.Rows[i][j].ToString() + "\"");
                                if (j < dt.Columns.Count - 1)
                                {
                                    Json.Append(",");
                                }
                            }
                            Json.Append("}");
                            if (i < dt.Rows.Count - 1)
                            {
                                Json.Append(",");
                            }
                        }
                    }
                    Json.Append("]}");
                }
            }
            return Json.ToString();
        }
        /// <summary>
        /// datatable translate to json data, please keep the datatable is not null or empty.
        /// </summary>
        /// <param name="jsonName"></param>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string DataTableToJson(string jsonName,System.Data.DataTable dt)
        {
            StringBuilder Json = new StringBuilder();
            Json.Append("{\"" + jsonName + "\":[");
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Json.Append("{");
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        Json.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":\"" + dt.Rows[i][j].ToString() + "\"");
                        if (j < dt.Columns.Count - 1)
                        {
                            Json.Append(",");
                        }
                    }
                    Json.Append("}");
                    if (i < dt.Rows.Count - 1)
                    {
                        Json.Append(",");
                    }
                }
            }
            Json.Append("]}");
            return Json.ToString();
        }

    }
}